package model

import (
	"global/structs"
	"fmt"
)

const COIN_PAIR_ENABLED = "1"
const COIN_PAIR_DISABLED = "0"

type coinPairModel struct {}

var CoinPairModel = new(coinPairModel)

// 获取交易对
func (coinPairModel) GetAll() ([]structs.CoinPair, error) {
	var err error
	coinPairs := make([]structs.CoinPair, 0)
	if err = MainDB.Where("enabled = ?", 1).Find(&coinPairs); err != nil {
		return nil, err
	}
	return coinPairs, err
}

// 获取一条记录
func (coinPairModel) GetFirst(coinPair string) (*structs.CoinPair, error) {

	item := new(structs.CoinPair)
	has, err := MainDB.Where("name=?", coinPair).Get(item)
	if err != nil {
		return nil, err
	}

	if !has {
		return nil, fmt.Errorf("no item found, coin pair: %s", coinPair)
	}

	return item, nil
}